<?php

class Store {

	protected $id = null;
	protected $path = null;
	protected $hash = array();

	public function __get($key) {
		if (isset($this->hash[$key])) {
			return $this->hash[$key];
		} else {
			return null;
		}
	}

	public function __set($key, $value) {
		$this->hash[$key] = $value;
	}

	public function __construct($id, $path, &$error = null) {

		$this->id = (!$id) ? md5(time()) : $id;

		$this->path = $path;

		if (!is_file(sprintf("%s%s", $this->path, $this->id))) {
			$error = true;
			return;
		}

		$object = unserialize(file_get_contents(sprintf("%s%s", $this->path, $this->id)));

		foreach ($object as $key => $value) {
			$this->{$key} = $value;
		}

		unset($object);

	}

	public function save() {
		file_put_contents(sprintf("%s%s", $this->path, $this->id), serialize($this));
	}

}